using System;
using System.Data;
using System.IO;

public class MainConfig
{
  DataSet _ds = null;	
  
  public MainConfig()
  {
    _ds = new DataSet();
    FileStream finschema = new FileStream("dreconfigs.xsd",FileMode.Open,FileAccess.Read,FileShare.Read);
    _ds.ReadXmlSchema(finschema);
    finschema.Close();
    finschema.Dispose();
    FileStream findata = new FileStream("dreconfigs.xml", FileMode.Open,FileAccess.Read,FileShare.ReadWrite);
    _ds.ReadXml(findata);
    findata.Close();
    findata.Dispose();
 }
  
  public void Fermer()
  {
  	_ds.Dispose();
  	_ds = null;
  }
  
  public void AddConfig(string cfgName,
                        string cfgfromServer,
                        string cfgfromDatabase,
                        string cfgfromUser,
                        string cfgfromPassword,
                        string cfgtoServer,
                        string cfgtoDatabase,
                        string cfgtoUser,
                        string cfgtoPassword)
  {
  	DataRow newrow = _ds.Tables[0].NewRow();
  	newrow["configName"] = cfgName;
    newrow["fromServer"] = cfgfromServer;
    newrow["fromDatabase"] = cfgfromDatabase;
    newrow["fromUser"] = cfgfromUser;
    newrow["fromPassword"] = cfgfromPassword;
    newrow["toServer"] = cfgtoServer;
    newrow["toDatabase"] = cfgtoDatabase;
    newrow["toUser"] = cfgtoUser;
    newrow["toPassword"] = cfgtoPassword;
    
  	_ds.Tables[0].Rows.Add(newrow);
  	FileStream fout = new FileStream("dreconfigs.xml",FileMode.Create,FileAccess.Write,FileShare.ReadWrite);
  	_ds.WriteXml(fout);
  	fout.Flush();
  	fout.Close();
  	fout.Dispose();

  	_ds.Clear();
  	FileStream findata = new FileStream("dreconfigs.xml", FileMode.Open,FileAccess.Read,FileShare.ReadWrite);
    _ds.ReadXml(findata);
    findata.Close();
    findata.Dispose();
  }
  
  public void DelConfig(string cfgName)
  {
   	foreach(DataRow dr in _ds.Tables[0].Rows)
  	{
   		if (dr["configName"].ToString()==cfgName)
   		{
   			dr.Delete();
   			break;
   		}
  	}	
    _ds.AcceptChanges();
	FileStream fout = new FileStream("dreconfigs.xml",FileMode.Create,FileAccess.Write,FileShare.ReadWrite);
	_ds.WriteXml(fout);
  	fout.Flush();
  	fout.Close();
  	fout.Dispose();
  	
  	_ds.Clear();
    FileStream findata = new FileStream("dreconfigs.xml", FileMode.Open,FileAccess.Read,FileShare.ReadWrite);
    _ds.ReadXml(findata);
    findata.Close();
    findata.Dispose();
  }
      
  public DataTable GetConfigs()
  {
  	return _ds.Tables[0];
  }
  
  public DataRow GetConfig(string cfgName)
  {
   	DataRow aRes = null;
   	
  	foreach(DataRow dr in _ds.Tables[0].Rows)
  	{
   		if (dr["configName"].ToString()==cfgName)
   		{
			aRes = dr;
   			break;
   		}
   	}
  	
  	return aRes;
  }
}
                        
  	
  	 